package net.hn.hnms.biz.safety.fire.company.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.hn.hnms.biz.safety.fire.Dao.ODSSensorMonitorDao;
import net.hn.hnms.biz.safety.fire.company.domain.vo.SGJCAlarmVo;
import net.hn.hnms.biz.safety.fire.domain.SensorMonitorPo;
import net.hn.hnms.biz.safety.fire.entity.AlarmVO;
import net.hn.hnms.biz.safety.fire.mapper.SensorMonitorMapper;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.redis.domain.SysOrganization;
import net.hn.hnms.sys.common.redis.domain.SysOrganizationVo;
import net.hn.hnms.sys.common.redis.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 智呈物联 nzw
 * @date 2022/12/29
 */
@Service
public class CompanyFireService {

    ODSSensorMonitorDao sensorMonitorDao;
    /**
     * 束管监测实时表Mapper
     */
    @Resource
    private SensorMonitorMapper sensorMonitorMapper;

    @Autowired
    public void setSensorMonitorDao(ODSSensorMonitorDao sensorMonitorDao) {
        this.sensorMonitorDao = sensorMonitorDao;
    }

    public Map<String,Object> alarmCurve(){

        Date end = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(end);
        calendar.add(Calendar.MONTH,-1);
        Date start = calendar.getTime();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<Map<String,String>> list = sensorMonitorDao.findAllAlarm(simpleDateFormat.format(start),simpleDateFormat.format(end));

        Map<String,String> data = time();
        for (Map<String,String> map:list){
            data.put(map.get("dataTime"),map.get("count"));
        }

        String[] xData = new String[data.size()];
        String[] alarm = new String[data.size()];
        int code = 0;
        for (String time:data.keySet()){
            xData[code] = time;
            alarm[code] = data.get(time);
            code++;
        }

        Map<String,Object> map = new HashMap<>();
        map.put("xData",xData);
        map.put("o2Value",alarm);
        return map;

    }

    public List<AlarmVO> alarm(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String start = simpleDateFormat.format(new Date());

        List<Map<String,Object>> list = sensorMonitorDao.findAlarms(start);

        List<AlarmVO> alarmVOList = new ArrayList<>();
        for (Map<String,Object> map : list){
            AlarmVO alarmVO = new AlarmVO();
            alarmVO.setDateTime(map.get("date_time")+"");
            alarmVO.setMineCode(map.get("mine_code")+"");
            alarmVO.setReason(map.get("point_type_name")+"");
            alarmVOList.add(alarmVO);
        }

        return alarmVOList;
    }

    private Map<String,String> time(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        Map<String,String> data = new LinkedHashMap<>();

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.WEEK_OF_YEAR,-1);

        for (;calendar.getTime().before(new Date());){
            data.put(simpleDateFormat.format(calendar.getTime()),"0.0000");
            calendar.add(Calendar.DAY_OF_YEAR,1);
        }
        return data;
    }

    /**
     * 获取今日束管监测报警列表(内因火灾)
     * @param companyCode
     * @return
     */
    public List<SGJCAlarmVo> getTodaySGJCAlarmList(String companyCode) {
        //获取当前公司的所有煤矿
        SysOrganizationVo sysOrganizationVo = RedisUtils.getCacheObject(Constants.SYS_CODESUB_KEY + companyCode);
        if (sysOrganizationVo == null) {
            throw new HuaNengException(Constants.FAIL, "无法获取当前公司的子煤矿信息，请检查词典"+companyCode);

        }
        List<SGJCAlarmVo> voList = new ArrayList<>();
        for (SysOrganizationVo organizationVo : sysOrganizationVo.getSubCodeList()) {
            List<SGJCAlarmVo> alarmListByMineCode = getAlarmListByMineCode(organizationVo.getOrgCode());
            for (SGJCAlarmVo sgjcAlarmVo : alarmListByMineCode) {
                voList.add(sgjcAlarmVo);
            }
        }
        return voList;
    }

    /**
     * 根据煤矿编码获取束管报警列表(内因火灾)
     * @param mineCode
     * @return
     */
    public List<SGJCAlarmVo> getAlarmListByMineCode(String mineCode) {
        //获取当前煤矿信息
        SysOrganization sysOrganization = RedisUtils.getCacheObject(Constants.SYS_CODE_KEY + mineCode);
        if (sysOrganization == null) {
            throw new HuaNengException(Constants.FAIL, "煤矿编码出错，请检查词典" + mineCode);

        }
        //获取今天的日期 LocalDate.now()
        //获取测点不为零且是今天的数据列表
        LambdaQueryWrapper<SensorMonitorPo> wrapper = new LambdaQueryWrapper<>();
        wrapper.ne(SensorMonitorPo::getPointStatus, 0);
        wrapper.eq(SensorMonitorPo::getMineCode, mineCode);
        wrapper.apply("DATE(data_time) = '" + LocalDate.now() + "'" );
        List<SensorMonitorPo> sensorMonitorPos = sensorMonitorMapper.selectList(wrapper);
        if (sensorMonitorPos.isEmpty()) {
            return new ArrayList<>();
        }
        return sensorMonitorPos.stream().map(t -> {
            SGJCAlarmVo sgjcAlarmVo = new SGJCAlarmVo();
            //测点编码
            sgjcAlarmVo.setPointCode(t.getPointCode());
            //煤矿编码
            sgjcAlarmVo.setMineCode(t.getMineCode());
            sgjcAlarmVo.setMineName(sysOrganization.getOrgName());
            //pointLocation
            sgjcAlarmVo.setPointLocation(t.getPointMonitorLocation());
            //测点类型名称
            sgjcAlarmVo.setComponentName(t.getPointTypeName());
            //监测值
            sgjcAlarmVo.setComponentValue(t.getPointValue().toString());
            //测点单位
            sgjcAlarmVo.setComponentUnit(t.getPointValueUnit());
            //数据时间
            sgjcAlarmVo.setDateTime(t.getDataTime());
            return sgjcAlarmVo;
        }).collect(Collectors.toList());

    }
}
